import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPreviewPage extends StatefulWidget{
  final File file;
  VideoPreviewPage(this.file);

  @override
  State<StatefulWidget> createState() {
    return VideoPreviewPageState();
  }

}

class VideoPreviewPageState extends State<VideoPreviewPage>{
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    _controller=VideoPlayerController.file(widget.file);
    _controller.setLooping(true);
    _controller.initialize();

  }

  //可能还需要加上播放进度条的控制
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("视频预览"),
      ),
      body: Center(
        child: FutureBuilder(
          future: Future.delayed(Duration(seconds: 2)),//暂时用2秒模拟加载，视频的比例才能初始化出来
          builder: (context,snapshot){
            if(snapshot.hasError){
              debugPrint("${snapshot.error.toString()}");
            }

            if(snapshot.connectionState==ConnectionState.done){
              _controller.play();

              return Stack(
                alignment: Alignment.center,
                children: [
                  AspectRatio(
                    aspectRatio: _controller.value.aspectRatio,
                    child: VideoPlayer(_controller),
                  ),
                ],
              );

            }else{
              return Center(
                child: CircularProgressIndicator(),
              );
            }

          },
        ),

      ),
    );
  }


  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }




}